<?php
class BaseController extends Zend_Controller_Action{
    
   public function init(){
    Zend_Loader::loadClass('AuthModel');
   }
   
   public function preDispatch(){
				
		$this->view->module = $this->_request->getModuleName();
		$this->view->controller = $this->_request->getControllerName();
		$this->view->action = $this->_request->getActionName();
		
		//Phan quyen
		$acl = new Zend_Acl();
      
//      //khai bao cac nhom (role)
        $acl
         ->addRole(new Zend_Acl_Role('Moderator'))
         ->addRole(new Zend_Acl_Role('Admin'));
//     
//      //khai bao danh sach cac controller
      $controller=array('index','type','admin');
      $acl->add(new Zend_Acl_Resource('index'));
      $acl->add(new Zend_Acl_Resource('type'));
      $acl->add(new Zend_Acl_Resource('admin'));
      $acl->add(new Zend_Acl_Resource('ads'));
      $acl->add(new Zend_Acl_Resource('tvonline'));
      $acl->add(new Zend_Acl_Resource('guide'));
      $acl->add(new Zend_Acl_Resource('news'));
      $acl->add(new Zend_Acl_Resource('settings'));
      $acl->add(new Zend_Acl_Resource('statistics'));
//      
//      //cap quyen truy cap cho cac nhom
      $acl->allow('Moderator',array('index','type','news'),array('index','add','logout','edit','doneedit'));
      
      $acl->allow('Admin',array('index','type','admin','ads','tvonline','guide','news','settings','statistics'),array('down-mp3','index','add','edit','doneedit','logout','delete','account','deleteacount','editacount','editstatus','category','sorttype','nomination','policy','contact'));
      
     
      //cam nhom truy cap
        $acl->deny('Moderator',array('admin'),array('deleteacount','editacount','editstatus'));   
        $this->_acl = $acl;
		$controller= $this->_request->getControllerName();
		$action = $this->_request->getActionName();
		
		//set layout
		
        /*	
		$auth = Zend_Auth::getInstance();
        if(!$auth->hasIdentity()){
            if ($this->_request->getActionName() != 'login') {
                $this->_redirect('/admin/index/login');
            }
        }else{*/
        
        /*tich hop*/
        $aut = Zend_Auth::getInstance();
        if ($aut->hasIdentity()) {
       	    $info = $aut->getIdentity();
        }
        /*---*/
        
        $auth = new AuthModel();
        // cho nay
        $info = $auth->getIdentity();
        if($info==0&&($info['name']!='admin_sunnet'||$info['pass']=!'@#sunnet123')){
			if ($this->_request->getActionName() != 'login') {
	            $this->_redirect('/admin/index/login');
	        }
	    }
	    else {//cho nay nua*/

	    $role=$auth->getIdentityField('role');
        //$role= 'Admin';
//      Zend_Debug::dump($role);die;
            if($acl->isAllowed($role,$controller,$action)){
               $layoutPath = TEMPLATE_PATH ."/admin"  ;
               $option = array (	'layout' 		=> 'index', 
	              	        'layoutPath' 	=> $layoutPath );
		       Zend_Layout::startMvc ( $option );
	    	}
	    	else {
	    		echo "<script type='text/javascript'>
	    					alert( 'Bạn không có quyền truy cập trang này');
	    			</script>";
		    	;                      
		    	exit();
	    	}
	    }//cho nay nay
		
	}
}